package com.chinasoft.service;

import com.chinasoft.controller.UserController;
import com.chinasoft.dao.UserDao;
import com.chinasoft.pojo.User;
import com.chinasoft.tools.DBUtil;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

/**
 * @Auther:hotfox2008
 * @Date:2024/6/29
 * @Description:com.chinasoft.service
 * @version:1.0
 * service层捕获异常，进行事务处理
 * 事务处理：调用不同dao的多个方法，必须使用同一个connection（connection作为参数传递）
 * 事务完成之后，需要在service层进行connection的关闭，在dao层关闭（PreparedStatement和ResultSet对象
 */
@Service
public class UserServiceImpl implements UserService{
    private static final Logger log = Logger.getLogger(UserServiceImpl.class);
    @Autowired
    private UserDao userDao;
    @Override
    public boolean addUser(User user) {
        boolean flag = false;
        Connection connection = null;
        try{
            connection = DBUtil.getConnection();
            connection.setAutoCommit(false);//开启JDBC手动事务管理
            int updateRows = userDao.addUser(connection,user);
            connection.commit();
            if(updateRows > 0){
                flag = true;
                log.info("add success!");
            }else {
                log.info("add failed!");
            }
        }catch (Exception e){
            e.printStackTrace();
            try {
                log.info("rollback==================>");
                connection.rollback();
            }catch (SQLException er){
                er.printStackTrace();
            }
        }finally {
            DBUtil.closeResource(connection,null,null);
        }
        return flag;
    }

    @Override
    public User login(String userCode, String userPassword) {
        Connection connection = null;
        User user = null;
        try{
            connection = DBUtil.getConnection();
            user = userDao.getLoginUser(connection,userCode);

        }catch (Exception e){
            e.printStackTrace();
        }finally {
            DBUtil.closeResource(connection,null,null);
        }

        return user;
    }

    @Override
    public List<User> getUserList(String queryUserName, int queryUserRole, int currentPageNo, int pageSize) {
        Connection connection = null;
        List<User> userList = null;
        log.info("queryUserName ---- > " + queryUserName);
        log.info("queryUserRole ---- > " + queryUserRole);
        log.info("currentPageNo ---- > " + currentPageNo);
        log.info("pageSize ---- > " + pageSize);
        try{
            connection = DBUtil.getConnection();
            userList = userDao.getUserList(connection,queryUserName,queryUserRole,currentPageNo,pageSize);

        }catch (Exception e){
            e.printStackTrace();
        }finally {
            DBUtil.closeResource(connection,null,null);
        }
        return userList;
    }

    @Override
    public int getUserCount(String queryUserName, int queryUserRole) {
        Connection connection = null;
        int count = 0;
        System.out.println("queryUserName ---- > " + queryUserName);
        System.out.println("queryUserRole ---- > " + queryUserRole);
        try {
            connection = DBUtil.getConnection();
            count = userDao.getUserCount(connection, queryUserName,queryUserRole);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }finally{
            DBUtil.closeResource(connection, null, null);
        }
        return count;
    }

    @Override
    public User selectUserCodeExist(String userCode) {
        Connection connection = null;
        User user = null;
        try {
            connection = DBUtil.getConnection();
            user = userDao.getLoginUser(connection, userCode);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }finally{
            DBUtil.closeResource(connection, null, null);
        }
        return user;
    }

    @Override
    public boolean deleteUserById(Integer delId) {
        Connection connection = null;
        boolean flag = false;
        try {
            connection = DBUtil.getConnection();
            if(userDao.deleteUserById(connection,delId) > 0)
                flag = true;
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }finally{
            DBUtil.closeResource(connection, null, null);
        }
        return flag;
    }

    @Override
    public User getUserById(String id) {
        // TODO Auto-generated method stub
        User user = null;
        Connection connection = null;
        try{
            connection = DBUtil.getConnection();
            user = userDao.getUserById(connection,id);
        }catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
            user = null;
        }finally{
            DBUtil.closeResource(connection, null, null);
        }
        return user;
    }

    @Override
    public boolean modify(User user) {
        Connection connection = null;
        boolean flag = false;
        try {
            connection = DBUtil.getConnection();
            if(userDao.modify(connection,user) > 0)
                flag = true;
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }finally{
            DBUtil.closeResource(connection, null, null);
        }
        return flag;
    }

    @Override
    public boolean updatePwd(int id, String pwd) {
        boolean flag = false;
        Connection connection = null;
        try{
            connection = DBUtil.getConnection();
            if(userDao.updatePwd(connection,id,pwd) > 0)
                flag = true;
        }catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
        }finally{
            DBUtil.closeResource(connection, null, null);
        }
        return flag;
    }
}
